<template>
  <div class="drinkMachindIndex">
    <!-- <c-title :hide="true" :text="headTitle"></c-title> -->
    <div class="tabsBox">
      <backBtn></backBtn>
      <van-tabs v-model="active" color="#015EF5" title-active-color="#015EF5" @change="tabsNav">
        <van-tab :title="item.type_name" v-for="item in activeTypeArr" :key="item.type_id"></van-tab>
      </van-tabs>
    </div>

    <div class="device">
      <div class="left">
        <div class="name">
          <span v-if="!nameEdit" @click="nameEditShow">{{ detailInfo.name }}</span>
          <input :autofocus="autofocus" v-model="nameTxt" ref="fieldInp" v-show="nameEdit" placeholder="请输入自定义设备名称" @blur="nameEditBlur" />
        </div>
        <div class="left_img" @click="nameEditShow" v-if="!nameEdit">
          <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_edit%402x.png" alt="" />
        </div>
      </div>
      <div class="left" @click="toDeviceManage">
        <div class="name" style="color: #aaaab3;">设备管理</div>
        <van-icon name="arrow" color="#AAAAB3" />
      </div>
    </div>
    <div class="overview">
      <div class="overview_t">
        <div class="name">设备总览</div>
        <div class="status">
          <div class="status_t">
            <div class="cirle"></div>
            <div class="status_t_name">{{ detailInfo.status_name || "--" }}</div>
          </div>
          <div class="status_t">
            <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_wifi@2x.png" alt="" />
            <div class="status_t_name">{{ detailInfo.online_status_name || "未连接" }}</div>
          </div>
          <div class="status_t">
            <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_off%402x.png" alt="" />
            <div class="status_t_name">{{ detailInfo.onoff == 1 ? "开启" : "未开启" }}</div>
          </div>
          <van-switch v-model="onoffStatus" size="18px" style="margin-left: 0.5rem;" @change="tapOnOff" v-if="(machine_type == 2 || machine_type == 4) && machine_id" />
        </div>
      </div>
      <div class="overview_b">
        <div class="overview_b_l" style="flex: 0.8;">
          <div class="overview_b_l_t">{{ detailInfo.day_count }}天</div>
          <div class="overview_b_l_b">已服务</div>
        </div>
        <div class="overview_b_l">
          <div class="overview_b_l_t">{{ detailInfo.machine_type_name || "--" }}</div>
          <div class="overview_b_l_b">设备类型</div>
        </div>
        <div class="overview_b_l" style="flex: 2; margin-left: 0.625rem;">
          <div class="overview_b_l_t">{{ detailInfo.local_code || "--" }}</div>
          <div class="overview_b_l_b">设备编号</div>
        </div>
      </div>
    </div>
    <!-- v-if="detailInfo.machine_type == 1" -->
    <water :detailInfo="detailInfo" :machineId="machine_id" v-if="(machine_type == 1 || machine_type == 3) && machine_id" @refresh="refreshChild" />
    <airCleaner v-if="(machine_type == 2 || machine_type == 4) && machine_id" :detailInfo="detailInfo" :machine_type="machine_type" :machineId="machine_id" @refresh="refreshChild" />
  </div>
</template>
<script>
import water from "./components/water.vue";
import airCleaner from "./components/airCleaner.vue";
import backBtn from "./components/backBtn.vue";
export default {
  data() {
    return {
      active: 0,
      headTitle: "",
      activeTypeArr: [],
      nameEdit: false,
      nameTxt: "",
      detailInfo: [],
      machine_id: "",
      onoffStatus:false,

      // 扫码连接带的machine_type，和id
      machine_type: "",
      id: ""
    };
  },
  components: { water, airCleaner, backBtn },
  activated() {
    if (this.$route.query.machine_type) {
      this.machine_type = this.$route.query.machine_type;
    }
    this.getData();
  },
  methods: {
    tapOnOff() {
      let json = {
        machine_id:this.machine_id,
        onoff: this.onoffStatus?1:0
      };
      $http.post("plugin.drink-machine.frontend.machine.openStatus", json, "加载中...").then(res => {
        if (res.result == 1) {
          this.getData("click");
        } else {
          this.$toast(res.msg);
        }
      });
    },
    tabsNav() {
      this.activeTypeArr.forEach((ele, ind) => {
        if (this.active == ind) {
          this.machine_type = ele.machine_type;
        }
      });
      this.getData("click");
    },
    refreshChild() {
      console.log("refreshChild");
      $http.post("plugin.drink-machine.frontend.machine.refreshMachine", { machine_id: this.machine_id }, "加载中...").then(res => {
        if (res.result == 1) {
          this.machineDetail(this.machine_id);
        } else {
          this.$toast(res.msg);
        }
      });
    },
    nameEditShow() {
      this.nameEdit = true;
      this.$nextTick(() => {
        this.$refs.fieldInp.focus();
      });
    },
    nameEditBlur() {
      this.nameEdit = false;
      if (this.detailInfo.name == this.nameTxt || !this.nameTxt) {
        return;
      }
      $http.post("plugin.drink-machine.frontend.machine.rename", { machine_id: this.machine_id, name: this.nameTxt }, "加载中...").then(res => {
        if (res.result == 1) {
          this.$toast(res.msg);
          this.detailInfo.name = this.nameTxt;
          this.nameTxt = "";
        } else {
          this.$toast(res.msg);
        }
      });
    },
    toDeviceManage() {
      console.log(this.activeTypeArr[this.active]);
      let type_id = this.activeTypeArr[this.active].type_id;
      this.$router.push(
        this.fun.getUrl("deviceManage", {
          id: type_id
        })
      );
    },
    getData(key) {
      $http.post("plugin.drink-machine.frontend.machine.machineTypeList", {}, "加载中...").then(res => {
        if (res.result == 1) {
          console.log(res.data);
          this.fun.setWXTitle(this.$store.state.temp.mailInfo.name ? this.$store.state.temp.mailInfo.name : "");
          this.headTitle = this.$store.state.temp.mailInfo.name ? this.$store.state.temp.mailInfo.name : "";
          this.activeTypeArr = res.data;
          if (res.data.length <= 0) {
            this.$dialog
              .alert({
                message: "暂无设备，请绑定设备后重试"
              })
              .then(() => {
                this.$router.go(-1);
              });
            return;
          }
          //   // 如果链接上有带类型id
          this.activeTypeArr.forEach((element, ind) => {
            if (element.type_id == this.machine_type) {
              this.active = ind;
            }
          });
          if (this.$route.query.id && !key) {
            this.machine_id = this.$route.query.id;
          } else {
            this.machine_id = this.activeTypeArr[this.active].machine_id;
          }
          this.machineDetail(this.machine_id);
          this.machine_type = this.activeTypeArr[this.active].type_id;
        } else {
          this.$toast(res.msg);
        }
      });
    },
    machineDetail(id) {
      $http.post("plugin.drink-machine.frontend.machine.machineDetail", { machine_id: id }, "加载中...").then(res => {
        if (res.result == 1) {
          this.detailInfo = res.data;
          this.onoffStatus = this.detailInfo.onoff == 1?true:false;
        } else {
          this.$toast(res.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.overview {
  margin: 0.5rem 0.75rem;
  background: #fff;
  border-radius: 0.75rem;
  margin-top: 0.5rem;
  padding: 0.88rem 0.75rem 1.06rem 0.75rem;
  box-sizing: border-box;

  .overview_b {
    margin-top: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    overflow: hidden;

    .overview_b_l {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: center;
      width: 100%;
      overflow: hidden;

      .overview_b_l_t {
        text-align: left;
        width: 100%;
        font-size: 1rem;
        color: #00001c;
        font-weight: bold;
        margin-bottom: 0.3rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .overview_b_l_b {
        color: #6e6e79;
        font-size: 0.75rem;
        font-weight: 400;
      }
    }
  }

  .overview_t {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .name {
      flex-shrink: 0;
    }

    .status {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .cirle {
        width: 0.38rem;
        height: 0.38rem;
        background: #1ac970;
        border-radius: 100%;
        margin-right: 0.31rem;
      }

      .status_t {
        display: flex;
        align-items: center;
        margin-left: 1rem;

        img {
          width: 1rem;
          height: 1rem;
          margin: 0;
          margin-right: 0.25rem;
        }
      }
    }
  }
}

.device {
  margin: 0.5rem 0.75rem;
  background: #fff;
  border-radius: 0.75rem;
  height: 3rem;
  padding: 0 0.88rem;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;

  .left {
    display: flex;
    align-items: center;
    font-size: 0.88rem;

    .name {
      font-weight: 500;
      color: #00001c;
      margin-right: 0.44rem;

      input {
        border: none;
        padding: 0;
        width: auto;
        color: #6e6e79;
      }
    }

    .left_img {
      width: 1rem;
      height: 1rem;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.drinkMachindIndex {
  .tabsBox {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fff;

    .van-tabs {
      flex: 1;
    }
  }
}
</style>
